package com.zhiche.lisa.lspm.controller;

import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.service.attach.AttachService;
import com.zhiche.lisa.lspm.service.license.LicenseServier;
import com.zhiche.lisa.lspm.vo.attach.AttachDetailVO;
import com.zhiche.lisa.lspm.vo.attach.LicenseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 附件 接口
 */
@RestController
@RequestMapping(value = "/licenseAttach")
@Api(value = "/licenseAttach", description = "证照附件接口", tags = {"证照附件接口"})
public class LicenseAttachController {
    private static final Logger LOGGER = LoggerFactory.getLogger(LicenseAttachController.class);

    @Autowired
    private AttachService attachService;
    @Autowired
    private LicenseServier licenseServier;

    /**
     * 根据证照类型和所属目标的id查询其中的附件列表
     * @param licenseVO {licenseTypeId：1,belongedId=10002,belongedType="lsp/carrier/.."}
     * @return
     */
    @PostMapping(value = "/queryAttachList")
    @ApiOperation(value = "获取附件", notes = "获取附件", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<List<AttachDetailVO>> queryAttachList(@RequestBody LicenseVO licenseVO) {
        LOGGER.info("LicenseAttachController.queryAttachList（创建承运商） params : {} ", licenseVO.toString());
        RestfulResponse<List<AttachDetailVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(),
                null);
        try {
            List<AttachDetailVO> lspLicenses = attachService.queryAttachList(licenseVO);
            result.setData(lspLicenses);
        } catch (BaseException e) {
            LOGGER.error("LicenseAttachController.queryAttachList ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("LicenseAttachController.queryAttachList ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 根据传入的附件详情获取创建后的承运商信息
     *
     *
     * @param attachDetailVO
     * {attachType:01(附件类型),attachKey:..(附件标识key值), attachName:test.jpg,licenseTypeId:1(所属的证照类型),belongedId:10030(所属夫级的id),belongedType:"lsp/driver"(所属夫级的标识)}
     * @return
     */
    @PostMapping(value = "/createAttach")
    @ApiOperation(value = "创建附件", notes = "创建附件", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<List<AttachDetailVO>> createAttach(@RequestBody AttachDetailVO attachDetailVO) {
        LOGGER.info("LicenseAttachController.createAttach（创建附件） params : {} ", attachDetailVO.toString());
        RestfulResponse<List<AttachDetailVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(),
                null);
        try {
            List<AttachDetailVO> lspLicenses = attachService.createAttach(attachDetailVO);
            result.setData(lspLicenses);
        } catch (BaseException e) {
            LOGGER.error("LicenseAttachController.createAttach ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("LicenseAttachController.createAttach ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 根据附件id删除对应的附件信息
     * @param attachDetailVO {belongedType="lsp",attachId=100002}
     * @return
     */
    @PostMapping(value = "/deleteAttach")
    @ApiOperation(value = "删除附件", notes = "删除附件", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<String> deleteAttach(@RequestBody AttachDetailVO attachDetailVO) {
        LOGGER.info("LicenseAttachController.deleteAttach（删除附件） params : {} ", attachDetailVO.toString());
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(),
                null);
        try {
            attachService.deleteAttach(attachDetailVO);
        } catch (BaseException e) {
            LOGGER.error("LicenseAttachController.deleteAttach ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("LicenseAttachController.deleteAttach ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 根据证照的所属类型id获取旗下的所有证照信息
     * @param licenseInVO {belongedId:100030(所属类型的id),belongedType:"lsp"(所属类型的标识)}
     * @return
     */
    @PostMapping(value = "/queryLicense")
    @ApiOperation(value = "查询证照列表", notes = "查询证照列表", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<LicenseVO> queryLicense(@RequestBody LicenseVO licenseInVO){
        LOGGER.info("LicenseAttachController.queryLicense（查询证照列表） params : {} ", licenseInVO.toString());
        RestfulResponse<LicenseVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(),
                null);
        try {
            LicenseVO licenseVO=licenseServier.queryLicenseByBelongedId(licenseInVO);
            result.setData(licenseVO);
        } catch (BaseException e) {
            LOGGER.error("LicenseAttachController.queryLicense ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("LicenseAttachController.queryLicense ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
    @PostMapping(value = "/updateLicense")
    @ApiOperation(value = "更新证照信息", notes = "更新证照信息", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<String> updateLicense (@RequestBody LicenseVO licenseInVO){
        LOGGER.info("LicenseAttachController.queryLicense（更新证照信息） params : {} ", licenseInVO.toString());
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(),
                null);
        try {
            licenseServier.updateLicense(licenseInVO);
        } catch (BaseException e) {
            LOGGER.error("LicenseAttachController.updateLicense ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("LicenseAttachController.updateLicense ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}
